home *** CD-ROM | disk | FTP | other *** search
- /*
- File: FixGraphicsState.c
-
- Contains: QuickDraw GX to PostScript conversion code.
- File contains routines for rebuilding
- the graphics state after save/restore things
- happen
-
- Version: Technology: Quickdraw GX 1.1.x
-
- Copyright: © 1990-1997 by Apple Computer, Inc., all rights reserved.
- */
-
- #include "GXToPSBuildConfig.h"
- #include <GXGraphics.h>
- #include "GXGraphicsPriv.h"
- #include <GXEnvironment.h>
- #include "GXToPostScript.h"
- #include "IOUtilities.h"
- #include "RDUtil.h"
- #include "FontHandler.h"
- #include "PublicPostScriptIE.h"
- #include "private.h"
- #include "PSIEResources.h"
- #include "GXErrors.h"
- #include "ShapeUtilities.h"
-
- #ifdef resumeLabel
- #undef resumeLabel
- #endif
- #define resumeLabel(exception)
-
-
- typedef struct {
- gxTransform theTransform;
- gxInk theInk;
- } TSavedState;
-
- /***************************************************
-
- Function: FixGraphicsState
-
- Function sees to it that the printer's nested graphics state
- agrees with the Imaging Engine's internal nested graphics state.
- The printer's state gets corrupted due to the somebody doing
- saves and restores (such as the font handler)
-
- hGlobals: Handle to the imaging engine globals
- didRestore: Boolean indicating that a restore was done.
- didSave: Boolean indicating that a save was done.
-
- If both didSave and didRestore are true, then it is assumed that
- the order was restore first and then save.
- (doing a save and then a restore would accomplish nothing)
-
- *****************************************************/
- OSErr FixGraphicsState(TIEGlobalsHdl hGlobals, Boolean didRestore, Boolean didSave)
- {
- OSErr status = noErr;
- long saveDepth;
- TIEGlobalsPtr pGlobals;
- TGstate *pGstate;
- TSavedState *savedGstates, *pSavedGstate;
- long i, depth;
-
-
- /*** If only a save was done, mark it in the graphics state ***/
- if (didSave && !didRestore) {
-
- pGlobals = *hGlobals;
- pGlobals->gStateNest[pGlobals->gStateDepth].flags |= eGstateDidSave;
-
- #if DEBUGLEVEL >= DEBUGFEEDBACK
- dprintf(trace, "Save done at level %d", pGlobals->gStateDepth);
- #endif
-
- } else if (didRestore) {
-
- /*** Find the depth of the save ***/
- saveDepth = 0;
- pGlobals = *hGlobals;
- pGstate = &(pGlobals->gStateNest[0]);
- depth = pGlobals->gStateDepth;
-
- while (!(pGstate->flags & eGstateDidSave)) {
-
- ++pGstate;
- ++saveDepth;
-
- #if DEBUGLEVEL > 1
- if (saveDepth > pGlobals->gStateDepth) {
- DebugStr("\pFatal Error, Imaging Engine told to do a restore when there was no save noted");
- return(-999);
- }//end if
- #endif
-
- }//end while
-
- #if DEBUGLEVEL >= DEBUGFEEDBACK
- dprintf(trace, "Restoring to level %d, Max: %d, didSave: %d", saveDepth, depth, didSave);
- #endif
-
- /*** If a save wasn't done, mark didSave for this level false ***/
- if (!didSave)
- pGstate->flags &= ~eGstateDidSave;
-
-
- /***
- Now output the graphics state from the save level on
- ***/
- if (saveDepth < depth) {
-
- /* Make copies of the graphics states from 1 after saveDepth to the end */
- status = PSSetWorkSpaceSize(hGlobals, (depth - saveDepth) * sizeof(TSavedState));
- nrequire(status, failed_WorkSpace);
-
- HLockHi((Handle)hGlobals); // In case graphics calls move memory.
- HLockHi((*hGlobals)->hWorkSpace);
- pGlobals = *hGlobals;
- savedGstates = (TSavedState*)(*(pGlobals->hWorkSpace));
- pSavedGstate = savedGstates;
- pGstate = &(pGlobals->gStateNest[saveDepth+1]);
- for (i = saveDepth+1; i <= depth; ++i) {
-
- pSavedGstate->theInk = pGstate->theInk;
- if (pSavedGstate->theInk != nil)
- GXCloneInk(pSavedGstate->theInk);
-
- pSavedGstate->theTransform = pGstate->theTransform;
- if (pSavedGstate->theTransform != nil)
- GXCloneTransform(pSavedGstate->theTransform);
-
- ++pSavedGstate;
- ++pGstate;
-
- }//end for
-
-
- /******
- Now do internal grestores of our state back to the save level
- We do this because the routines that affect the output graphics state
- such as TransformDrone and ColorPrimitive check against the stored, nested
- graphics state which we are trying to recreate on the printer. So the easiest
- way to re-create it on the printer since it has been destroyed on the printer
- is to destroy the internal copy of it as well and then regenerated in the
- normal manner.
- ********/
- for (i = saveDepth+1; i <= depth; ++i) {
-
- status = DoGrestore(hGlobals, true);
- nrequire(status, failed_Grestore);
-
- }//end for
-
- /*** Now regenerate the graphics states from saveDepth + 1 on ****/
-
- (*hGlobals)->ieStateFlags |= eInitializeGstate;
-
- pSavedGstate = savedGstates;
- for (i = saveDepth + 1; i <= depth; ++i) {
-
- /* Do a gsave and output the transform */
-
- nrequire(status = DoGsave(hGlobals, false), failed_Gsave);
- nrequire(status = TransformDrone(hGlobals, pSavedGstate->theTransform), failed_Transform);
-
- #if DEBUGLEVEL >= DEBUGFEEDBACK
- {
- unsigned char pointsLeft[] = "%- points left: ";
- TRDParams* rdParams = pGlobals->pRDParams;
- nrequire(status = PSIEBufferData(hGlobals, pointsLeft, 16, 0), failed_Transform);
- pGlobals = *hGlobals;
- rdParams->resIndex = kDoInt;
- nrequire(status = RDResPrintf(rdParams, pGlobals->gStateNest[pGlobals->gStateDepth].pointsAvail), failed_Transform);
- }
- #endif
-
- /* Now do the ink for this level */
-
- status = InkDrone(hGlobals, pSavedGstate->theInk);
- nrequire(status, failed_Ink);
-
- /** And dispose of the transform and ink we copied **/
- GXDisposeInk(pSavedGstate->theInk);
- GXDisposeTransform(pSavedGstate->theTransform);
-
- ++pSavedGstate;
-
- }//end for
-
-
- failed_Ink:
- failed_Transform:
- failed_Gsave:
- failed_Grestore:
- {
- OSErr saveStatus = PSReleaseWorkSpace(hGlobals);
- if (saveStatus != noErr) status = saveStatus;
- }
-
- }//end if
-
- /*** Now recreate the Graphics State style stuff ***/
- if (status == noErr) {
-
- (*hGlobals)->ieStateFlags |= (eStyleOutOfDate + eFontOutOfDate);
-
- status = StyleDrone(hGlobals, (*hGlobals)->theStyle);
- nrequire(status, failed_Style);
-
- }//end if
-
- }//end if
-
-
-
- failed_Style:
-
- failed_WorkSpace:
-
- return(status);
-
- }//FixGraphicsState
-
-
-
-